package Game;

import java.util.*;

public class Packet {
    private boolean packed;
    private Vector keystroke = new Vector();
    private Vector position = new Vector();
    private boolean send_packet;
    private boolean isReady = true;
    private boolean isActionRelevant = false;
    private long now;
    private int index;                                                        //Client index
    private int key_size = 0;
    private int spot_size;
    public static final String[] SEPARATOR = {" ", ",", "!", "/"};

    /**
     *
     * @param index index of sending player
     * @param freq number of positions (per frame)
     */
    public Packet(int index, int freq) {
        this.packed = false;
        this.send_packet = true;
        this.index = index;
        this.now = Timestamp.getNow();
        setSpotSize(freq);
    }

    public Packet(String ID_line) {                                             //String Returned By ToString-Method
        this.send_packet = false;
        this.packed = true;

        String[] split = ID_line.split(SEPARATOR[0]);

        setIndex(Integer.parseInt(split[0]));
        setSpotSize(Integer.parseInt(split[1]));
        setKeySize(Integer.parseInt(split[2]));

        for (int i = 0; i < getSpotSize(); i++){
            position.addElement(new NodePosition(split[3].split(SEPARATOR[1])[i]));
        }
        
        if (getKeySize() != 0){
            setActionRelevant(true);
            for (int i = 0; i < getKeySize(); i++){
                keystroke.addElement(new KeyStroke(split[4].split(SEPARATOR[1])[i]));
            }
        }
    }

    public void pack(){
        while (!isReady){
            try {
                Thread.sleep(1);
            }
            catch (InterruptedException ex){
                ex.printStackTrace();
            }
        }
        setKeySize(keystroke.size());
        packed = true;
    }

    public boolean isPacked() {
        return packed;
    }

    public boolean isActionRelevant() {
        return isActionRelevant;
    }

    public void setActionRelevant(boolean isActionRelevant) {
        this.isActionRelevant = isActionRelevant;
    }

    @Override
    public String toString() {
        this.pack();

        StringBuffer line = new StringBuffer();

        line.append(index);
        line.append(SEPARATOR[0]);
        line.append(getSpotSize());
        line.append(SEPARATOR[0]);
        line.append(getKeySize());
        line.append(SEPARATOR[0]);

        for (int i = 0; i < getSpotSize(); i++){
            line.append(position.elementAt(i).toString());
            line.append(SEPARATOR[1]);
        }
        line.setLength(line.length()-1);
        line.append(SEPARATOR[0]);

        for (int i = 0; i < getKeySize(); i++){
            line.append(keystroke.elementAt(i).toString());
            line.append(SEPARATOR[1]);
        }
        line.setLength(line.length()-1);
        line.append(SEPARATOR[0]);

        return line.toString();

    }

    public int getKeySize(){
        if (packed){
            return key_size;
        }
        else {
            return -1;
        }
    }

    private void setKeySize(int size){
        this.key_size = size;
    }

    public int getSpotSize(){
        if (packed){
            return spot_size;
        }
        else {
            return -1;
        }
    }

    private void setSpotSize(int size){
        this.spot_size = size;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    // <editor-fold defaultstate="collapsed" desc="Adder">
    public void addKeyStroke(KeyStroke k) throws PacketAlreadyPackedException {
        isReady = false;
        if (send_packet && !packed) {
            k.setTimestamp(k.getTimestamp() - now);
            keystroke.addElement(k);
        } else if (packed) {
            throw new PacketAlreadyPackedException("Packet is already packed");
        }
        isReady = true;
    }

    public void addPosition(NodePosition current_position) throws PacketAlreadyPackedException {
        if (send_packet && !packed) {
            this.position.addElement(current_position);
        } else if (packed) {
            throw new PacketAlreadyPackedException("Packet is already packed");
        }
    }// </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Reader">
    public KeyStroke readKeyStroke(int index) {
        return (KeyStroke) keystroke.elementAt(index);
    }

    public NodePosition readPosition(int index) {
        return (NodePosition) position.elementAt(index);
    }// </editor-fold>


}



